package com.chzc.fm.duotin.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

/**
 * 数据库帮助类
 * Created by BillSinla on 2016/6/30.
 */
public class HistoryHelper extends SQLiteOpenHelper{
    private static final String DB_NAME = "history_db";

    public static final String TABLE_NAME = "history";

    private static final int VERSION = 1;

    //创建线程表保存线程的状态
    private static final String SQL_CREATE = "create table thread_info(_id integer primary key autoincrement," +
            "thread_id integer, " +
            "url text, " +
            "start long, " +
            "end long, " +
            "finished long)";

    private static final String SQL_DROP = "drop table if exists thread_info ";

    //创建专辑的表
    private static final String SQL_CREATE_ALBUM = "create table album(_id integer primary key autoincrement," +
            "album_id text UNIQUE, " +
            "title text, " +
            "image_url text, " +
            "content_num text)";

    private static final String SQL_DROP_ALBUM = "drop table if exists album";

    private static final String SQL_CREATE_CONTENT = "create table content(_id integer primary key autoincrement," +
            "content_id text UNIQUE, " +
            "display_order text, " +
            "title text, " +
            "album_id text, " +
            "updated text, " +
            "audio_32_size text," +
            "audio_64_size text, " +
            "duration text, " +
            "play_num text, " +
            "audio_32_url text," +
            "audio_64_url text," +
            "image_url text)";

    private static final String SQL_DROP_CONTENT = "drop table if exists content";


    public HistoryHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE);
        db.execSQL(SQL_CREATE_ALBUM);
        db.execSQL(SQL_CREATE_CONTENT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DROP);
        db.execSQL(SQL_CREATE);
        db.execSQL(SQL_DROP_ALBUM);
        db.execSQL(SQL_CREATE_ALBUM);
        db.execSQL(SQL_DROP_CONTENT);
        db.execSQL(SQL_CREATE_CONTENT);
        if(oldVersion == 1 && newVersion == 2){

        }
    }

    public static class HistoryTable implements BaseColumns{
        public static final String COLUMNS_RNAME = "rname";
        public static final String COLUMNS_DES = "des";
        public static final String COLUMNS_IMAGE_URL = "image_url";
        public static final String COLUMNS_MP3_URL = "mp3_url";
        public static final String COLUMNS_TIME = "time";

        public static String TABLE_SQL;

        static {
            //创建表的sql语句
            StringBuffer buffer = new StringBuffer();
            buffer.append("create table" + TABLE_NAME + "(");
            buffer.append(_ID + "integer primary key autoincrement" );
            buffer.append(COLUMNS_RNAME + "varchar(50),");
            buffer.append(COLUMNS_DES + "varchar(30),");
            buffer.append(COLUMNS_IMAGE_URL + "varchar(500),");
            buffer.append(COLUMNS_MP3_URL + "varchar(500),");
            buffer.append(COLUMNS_TIME + "long)");

            TABLE_SQL = buffer.toString();
        }
    }
}
